package com.moudle.app.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.support.v4.app.FragmentManager;

import com.moudle.app.R;
import com.moudle.app.base.BaseFragMent;
import com.moudle.app.base.BaseActivity;
import com.moudle.app.common.StringUtils;
import com.moudle.app.common.UIHelper;


/**
 * @Description 验证码登陆
 * @Author Li Chao
 * @Date 2015/12/10 11:23
 */
public class LoginCodeFragment extends BaseFragMent {
    private TextView mTvVoiceCodeLogin;// 语音验证码
    private Button mBtnLogin;// 登陆
    private EditText mEtUsername;
    private EditText mEtLoginCode;
    private Button mBtnGetMessage;// 获取验证码
    private ImageButton mBtnBack;// 回退按钮
    private String mUsername;
    private String mCode;
    private InputMethodManager mImm;
    private AlertDialog mAlertDlg = null;
    private FragmentManager fm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.frament_code_login, container, false);
        mAlertDlg = new AlertDialog.Builder(getActivity()).create();
        fm = getActivity().getSupportFragmentManager();
        mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        initView(v);
        initData();
        return v;
    }

    public void initView(View v) {
        //((TextView) v.findViewById(R.id.acitivity_name)).setText(R.string.login_message);
       // v.findViewById(R.id.btn_register).setVisibility(View.GONE);

        mEtUsername = (EditText) v.findViewById(R.id.login_username);
        mEtLoginCode = (EditText) v.findViewById(R.id.login_code);

        mBtnLogin = (Button) v.findViewById(R.id.btn_login);
        mBtnGetMessage = (Button) v.findViewById(R.id.get_message);
      //  mBtnBack = (ImageButton) v.findViewById(R.id.btn_back);
        mTvVoiceCodeLogin = (TextView) v.findViewById(R.id.yuyin_login);
        mBtnLogin.setOnClickListener(mCodeLister);
        mBtnGetMessage.setOnClickListener(mCodeLister);
        mTvVoiceCodeLogin.setOnClickListener(mCodeLister);
        mBtnBack.setOnClickListener(mCodeLister);
        // 打开软键盘
        mImm.showSoftInput(mEtUsername, InputMethodManager.RESULT_SHOWN);
        mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    View.OnClickListener mCodeLister = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.get_message:
                    mUsername = mEtUsername.getText().toString().trim();
                    mCode = mEtLoginCode.getText().toString().trim();
                    if (StringUtils.isEmpty(mUsername)) {
                        ((BaseActivity) getActivity()).errorPrompt(mEtUsername, R.string.login_phone_prompt);
                        return;
                    }
                    if (!StringUtils.isPhoneNum(mUsername)) {
                        ((BaseActivity) getActivity()).errorPrompt(mEtUsername, R.string.login_correct_phone);
                        return;
                    }
                    mAlertDlg.show();
                    UIHelper.showLoadingDialog(mAlertDlg, getActivity(), R.string.login_dialog_loading);
                    break;
                case R.id.yuyin_login:

                    break;
            }
        }
    };
}
